package com.mulanweb.api.module.controller.admin;

import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.controller.BaseController;
import com.mulanweb.api.module.model.ExtensionPersonnel;
import com.mulanweb.db.common.SQLGenerateHelper;

import java.util.Date;

/**
 * Created by Administrator on 2016/11/8 0008.
 */
public class ExtensionPersonnelController extends BaseController {

    public void search() {
        String name = getPara("name");
        SQLGenerateHelper helper = new SQLGenerateHelper("extension_personnel","create_time desc");
        helper.addLikeCondition("name",name);
        Page<ExtensionPersonnel> page = ExtensionPersonnel.dao.paginate(getPageNumber(),getPageSize(),helper.getSqlSelect(),helper.getSqlExceptSelect(),helper.getQueryParas());
        returnPageModelJson(page);
    }

    public void save(){
        Integer adminId = getSessionAttr("adminId");
        if(adminId ==null){
            returnJson(-1,"管理员未登录");
            return;
        }
        String name = getPara("name");
        String phone = getPara("phone");
        String remarks = getPara("remarks");
        ExtensionPersonnel extensionPersonnel = ExtensionPersonnel.dao.findFirst("SELECT * FROM extension_personnel WHERE name = ?",name);
        if(extensionPersonnel !=null ){
            returnJson(-1,"此姓名已存在！");
            return;
        }
        extensionPersonnel = new ExtensionPersonnel();
        extensionPersonnel.setName(name);
        extensionPersonnel.setPhone(phone);
        extensionPersonnel.setAdminId(adminId);
        extensionPersonnel.setRemarks(remarks);
        extensionPersonnel.setCreateTime(new Date());
        if(extensionPersonnel.save()){
            returnSuccessJson();
        }else{
            returnFailureJson();
        }


    }
}


